package com.common.base.widget.indicator

import android.animation.ValueAnimator
import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.View
import android.view.animation.AccelerateInterpolator
import com.common.base.R
import com.common.base.utils.AppUtils

class BannerIndicatorView : IndicatorView {

    constructor(context: Context) : super(context)
    constructor(context: Context, attrs: AttributeSet?, defStyle: Int) : super(context, attrs, defStyle)
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)

    override fun createIndicatorItemView(): IndicatorItemView {
        val view = BannerIndicatorItemView(context)
        view.create()
        return view
    }

    class BannerIndicatorItemView : IndicatorItemView {

        private val indicatorHeight = AppUtils.getContext().resources.getDimension(R.dimen.dp_4).toInt()
        private val indicatorWidthSelect = AppUtils.getContext().resources.getDimension(R.dimen.dp_4).toInt()
        private val indicatorWidthUnSelect = AppUtils.getContext().resources.getDimension(R.dimen.dp_4).toInt()

        constructor(context: Context) : super(context)
        constructor(context: Context, attrs: AttributeSet?, defStyle: Int) : super(context, attrs, defStyle)
        constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)

        override fun create() {
            val view = View(context)
            val lp = LayoutParams(indicatorWidthUnSelect, indicatorHeight)
            val lf = AppUtils.getContext().resources.getDimension(R.dimen.dp_2).toInt()
            lp.leftMargin = lf
            lp.rightMargin = lf
            view.setBackgroundResource(R.drawable.shape_dot_banner_indicator_normal)
            addView(view, lp)
        }

        override fun select(isAnimation: Boolean) {
            if(isSelected)
                return
            isSelected = true
            val view = getChildAt(0)
            view?.let {
                if(isAnimation) {
                    startSelectAnimation(it)
                    return@let
                }
                val lp = it.layoutParams as LayoutParams
                lp.width = indicatorWidthSelect
                view.setBackgroundResource(R.drawable.shape_dot_banner_indicator_selected)
                view.layoutParams = lp
            }
        }

        override fun unselect(isAnimation: Boolean) {
            if(!isSelected)
                return
            isSelected = false
            val view = getChildAt(0)
            view?.let {
                if(isAnimation) {
                    startUnSelectAnimation(it)
                    return@let
                }
                val lp = it.layoutParams as LayoutParams
                lp.width = indicatorWidthUnSelect
                view.setBackgroundResource(R.drawable.shape_dot_banner_indicator_normal)
                view.layoutParams = lp
            }
        }

        private fun startSelectAnimation(view: View) {
            startAnimation(view, indicatorWidthUnSelect, indicatorWidthSelect)
            view.setBackgroundResource(R.drawable.shape_dot_banner_indicator_selected)
        }

        private fun startUnSelectAnimation(view: View) {
            startAnimation(view, indicatorWidthSelect, indicatorWidthUnSelect)
            view.setBackgroundResource(R.drawable.shape_dot_banner_indicator_normal)
        }

        private fun startAnimation(view: View, start: Int, end: Int) {
            view.clearAnimation()
            val animationScale = ValueAnimator.ofInt(start, end)
            animationScale.addUpdateListener {
                val v = it.animatedValue as Int
                val lp = view.layoutParams as LayoutParams
                lp.width = v
                view.layoutParams = lp
            }
            animationScale.duration = 300
            animationScale.interpolator = AccelerateInterpolator()
            animationScale.start()
        }

    }
}